#include "statinfo.h"
#include "remotedef.h"
#include <QVariant>
#include <QStringList>

StatInfo::StatInfo()
    :BaseResp()
{
}

bool compareInfo(const QList<QString> &value1, const QList<QString> &value2)
{
    if ((value1.at(0) != "视频发送"
                && value1.at(0) != "音频接收"
                && value1.at(0) != "音频发送"
                && (value1.at(0) != "文档"))
               )
    {
        return true;
    }
    else if ((value2.at(0) != "视频发送"
              && value2.at(0) != "音频接收"
              && value2.at(0) != "音频发送"
              && (value2.at(0) != "文档")))
    {
        return false;
    }
    else if ( (value1.at(0) == "文档" && value1.at(7) == "down"))

    {
        return true;
    } else if ((value2.at(0) == "文档" && value2.at(7) == "down"))
    {
        return false;
    } else if (value1.value(0).contains("视频") && !value2.value(0).contains("视频"))
    {
        return true;
    } else if (value1.value(0).contains("文档") && !value2.value(0).contains("视频"))
    {
        return true;
    } else if ((value1.value(0) == "音频接收") && (!value2.value(0).contains("视频")
                                                 || !value2.value(0).contains("文档")))
    {
        return true;
    }else
    {
        return false;
    }
}

void StatInfo::unserialize(QString json)
{
    BaseResp::unserialize(json);
    if (m_json.isValid())
    {
        QList<OSDJson> list = m_json.getObjectArray();
        for(int i = 0; i < list.count(); i++)
        {
            OSDJson obj = list.at(i);
            if (obj.isValid())
            {
                QList<QString> st;

                int dir = obj.value(KEY_STAT_DIRECT).toInt();
                int type = obj.value(KEY_STAT_TYPE).toInt();

                QString updown = (dir == 0 ? "up":"down");
                QString direct = "";
                if (type == 0)
                {   //音频
                    direct = QString("音频") + QString((dir == 0 ? "发送":"接收"));
                }
                else if(type == 1)
                {
                    QString name = obj.value(KEY_STAT_NAME).toString();
                    if (dir == 1)
                    {
                        direct = name;
                    }
                    else
                    {
                        direct = "视频发送";
                    }
                }
                else if (type == 2)
                {
                    direct="文档";
                }
                QString protocol = obj.value(KEY_STAT_PROTOCOL).toString();
                QStringList ps = obj.value(KEY_STAT_PROTOCOL).toString().split(" ");
                if (ps.count() > 0)
                {
                    protocol = ps[0];
                }

                QString actual_rate = QString::number(obj.value(KEY_ACTUAL_RATE).toString().toDouble(),
                                                      'f',
                                                      2);

                QString format = "";
                int height = obj.value(KEY_VEDIO_HEIGHT).toInt();
                if (height == 1080
                   || height == 1088)
                {
                    format = "1080P";
                }
                else if (height == 0)
                {
                    format = "-";
                }
                else
                {
                    format = QString::number(height) + "P";
                }

                QString fps = QString::number(obj.value(KEY_STAT_FPS).toString().toDouble(),
                                              'f',
                                              2);
                if (type == 0)  //音频没有帧率
                {
                    fps = "-";
                }

                QString loss_rate = QString("%1%(%2)").
                                    arg(obj.value(KEY_STAT_PACKAGE_LOST_RATE).toString().toInt()).
                                    arg(obj.value(KEY_STAT_PACKAGE_LOST).toInt());

                QString encrpy = obj.value(KEY_STAT_SAFE).toInt() == 1 ? "加密":"非加密";
                st << direct << protocol << actual_rate << format << fps << loss_rate << encrpy << updown;
                m_statList.append(st);
            }
        }
        if (m_statList.count() > 1)
        {
            std::sort(m_statList.begin(), m_statList.end(),compareInfo);
        }
    }
}
QList<QList<QString> > StatInfo::statList() const
{
    return m_statList;
}

void StatInfo::setStatList(const QList<QList<QString> > &statList)
{
    m_statList = statList;
}

